﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
//new
using System.IO;

namespace JKZP
{
    public partial class editroad : Form
    {
        public string roadid;
        public string roadname;
        public string gbdm;
        public string bmbh;
        public string user;

        public int rowindex;

        public editroad()
        {
            InitializeComponent();
        }

        private void editroad_Load(object sender, EventArgs e)
        {
            textBox_id.Text = roadid;
            textBox_name.Text = roadname;
            textBox_gbdm.Text = gbdm;
            textBox_bmbh.Text = bmbh;
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            if (textBox_id.Text == "")
            {
                toolStripStatusLabel1.Text = "修改失败，路口编号不能为空";
                return;
            }
            if (textBox_id.Text.Length != 2)
            {
                toolStripStatusLabel1.Text = "修改失败，路口编号长度为2";
                return;
            }
            if (textBox_name.Text == "")
            {
                toolStripStatusLabel1.Text = "修改失败，路口名称不能为空";
                return;
            }
            if (textBox_gbdm.Text == "")
            {
                toolStripStatusLabel1.Text = "修改失败，国标代码不能为空";
                return;
            }
            //if (textBox_bmbh.Text == "")
            //{
            //    toolStripStatusLabel1.Text = "修改失败，部门编号不能为空";
            //    return;
            //}
            //查找编号是否已经存在
            DataGridView dgv = ((RoadForm)(this.Owner)).dataGridView1;
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                DataGridViewRow r = dgv.Rows[i];
                if ((textBox_id.Text == r.Cells["roadid"].Value.ToString()) && (i != rowindex))
                {
                    toolStripStatusLabel1.Text = "修改失败,编号已存在";
                    return;
                }
            }
            if (EditRoad(textBox_id.Text, textBox_name.Text,textBox_gbdm.Text,textBox_bmbh.Text,user) == true)
            {
                dgv.Rows[rowindex].Cells["roadid"].Value = textBox_id.Text;
                dgv.Rows[rowindex].Cells["roadname"].Value = textBox_name.Text;
                dgv.Rows[rowindex].Cells["gbdm"].Value = textBox_gbdm.Text;
                dgv.Rows[rowindex].Cells["bmbh"].Value = textBox_bmbh.Text;
                
                ((Form_Main)this.Owner.Owner).UpDateTree();

                if (PP.gISWebVersion == false)
                {
                    toolStripStatusLabel1.Text = "修改成功";
                }
                else
                {
                    if (PP.kdws.JKZP_UploadRoad(File.ReadAllText(PP.gRoadFile)) == true)
                    {
                        PP.ReLoad();
                        toolStripStatusLabel1.Text = "修改成功";
                        
                    }
                    else
                    {
                        toolStripStatusLabel1.Text = "修改失败";
                    }
                }
            }
            else
            {
                toolStripStatusLabel1.Text = "修改失败";
            }
        }

        private void button_cancle_Click(object sender, EventArgs e)
        {
            Close();
        }

        private bool EditRoad(string roadid, string roadname,string gbdm,string bmbh,string user)
        {
            //<?xml version="1.0" encoding="UTF-8"?>
            //<param>
            //<road roadid="01" gbdm="123456789" roadname="测试路口一">
            //    <camera ip="172.16.22.252" type="HK9121" direction="由东向西" workmode="" bz=""></camera>
            //    <camera ip="172.16.22.250" type="HK9151" direction="由南向北" workmode="" bz=""></camera>
            //</road>
            //</param>

            string xpath = "descendant::road[@roadid='" + roadid + "']";
            string[] key = new string[6];
            string[] value = new string[6];
            key[0] = "roadid";
            key[1] = "gbdm";
            key[2] = "roadname";
            key[3] = "bmbh";
            key[4] = "user";
            key[5] = "luduan";
            value[0] = roadid;
            value[1] = gbdm;
            value[2] = roadname;
            value[3] = bmbh;
            value[4] = user;
            value[6] = "";
            bool ans = PP.SetConfig(PP.gRoadFile, xpath, "", key, value);
            if (ans == true)
            {
                PP.EditRoad(roadid, roadname, gbdm,bmbh,user);
            }
            return ans;
        }
    }
}
